package com.cdc.widget;

import com.cdc.customviewtest3.R;
import com.cdc.util.MeasureUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {
	private Paint mPaint;
	
	private Context mContext;
	
	private Bitmap bitmap;
	
	private AvoidXfermode avoidXfermode;
	
	private int x, y, w, h;
	
	public CustomView(Context context) {
		this(context, null);
	}

	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initPaint();
		initRes(context);
	}

	private void initPaint() {
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		avoidXfermode = new AvoidXfermode(0XFFFFFFFF, 0, AvoidXfermode.Mode.TARGET);
		
	}
	
	private void initRes(Context context) {
		bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
		x = MeasureUtil.getScreenSize((Activity) mContext)[0] / 2 - bitmap.getWidth() / 2;
		y = MeasureUtil.getScreenSize((Activity) mContext)[1] / 2 - bitmap.getHeight() / 2;
		w = MeasureUtil.getScreenSize((Activity) mContext)[0] / 2 + bitmap.getWidth() / 2;
		h = MeasureUtil.getScreenSize((Activity) mContext)[1] / 2 + bitmap.getHeight() / 2;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(bitmap, x, y, mPaint);
//		mPaint.setARGB(255, 211, 53, 243);
//		mPaint.setXfermode(avoidXfermode);
		canvas.drawRect(x, y, w, h, mPaint);
	}

}